#include"variablenode.h"
#include"integernode.h"

NameStorage VariableNode::names;

//VariableNode::VariableNode() : var(0) {}
	
VariableNode::VariableNode(VariableNode const & v) : var(v.var) {}
	
VariableNode::VariableNode(string const & s, bool rename) : var(rename ? names.addOrRename(s) : names.addName(s)) {}

VariableNode::~VariableNode() {}
	
bool VariableNode::operator== (VariableNode const & v) const {
	return (var == v.var);
}

string VariableNode::toString() const {
	return names[var];
}

ExpressionNode * VariableNode::copy() const {
	return new VariableNode(*this);
}

bool VariableNode::isFreeVar(VariableNode const & v) const {
	return (var == v.var);
}

VariableNode VariableNode::rename() {
	return VariableNode(names[var], true);
}

bool VariableNode::isBoundVar(VariableNode const & v) const {
	return false;
}

ExpressionNode * VariableNode::eval(ProgramNode const & state) const {
	ExpressionNode * exp = state.expand(*this);
	ExpressionNode * res = exp -> eval(state);
	delete exp;
	return res;
}

ProgramNode * VariableNode::defun() const {
	return 0;
}

ExpressionNode * VariableNode::subst(VariableNode const & v, ExpressionNode const * e) {
	if (var == v.var)
		return e -> copy();
	else
		return copy();
}

IntegerNode VariableNode::deint() const {
	return IntegerNode(0, true);
}
